package cn.taneroom.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * IO流处理相关帮助类
 * @author TaneRoom
 * @since 2016年8月2日 下午3:20:57
 */
public class IOUtil {

	/**
	 * 二进制流的输出输出
	 * @param sourceFilePath 源文件地址
	 * @param targetFilePath 输出文件地址
	 * @throws IOException 强制调用函数捕获异常
	 */
	public static void byteReadAndWriter(String sourceFilePath, String targetFilePath) throws IOException{
		String subfix = sourceFilePath.substring(sourceFilePath.lastIndexOf("."), sourceFilePath.length());
		if(subfix != null || !"".equals(subfix)){
			//拼接上源文件的后缀，防止使用者忘记写后缀或者后缀写错，造成文件不能复制黏贴
			targetFilePath = targetFilePath.substring(0, targetFilePath.lastIndexOf("."))+subfix;
		}
		
		InputStream is = new FileInputStream(sourceFilePath);
		OutputStream os = new FileOutputStream(targetFilePath);
		int len = 1024;
		byte[] bytes = new byte[len];
		if(is.read(bytes, 0, len) != -1){
			//读取字节，然后输出，输出之后立马刷新
			os.write(bytes, 0, len);
			os.flush();
		}
		os.close();
		is.close();
	}
	
	/**
	 * 字符流的输入输出
	 * @param sourceFilePath 源文件地址
	 * @param targetFilePath 输出文件地址
	 * @throws IOException 强制调用函数捕获异常
	 */
	public static void charReadAndWriter(String sourceFilePath, String targetFilePath) throws IOException{
		String subfix = sourceFilePath.substring(sourceFilePath.lastIndexOf("."), sourceFilePath.length());
		if(subfix != null || !"".equals(subfix)){
			//拼接上源文件的后缀，防止使用者忘记写后缀或者后缀写错，造成文件不能复制黏贴
			targetFilePath = targetFilePath.substring(0, targetFilePath.lastIndexOf("."))+subfix;
		}
		
		BufferedReader br = new BufferedReader(new FileReader(sourceFilePath));
		BufferedWriter bw = new BufferedWriter(new FileWriter(targetFilePath));
		String s;
		while((s=br.readLine()) != null){
			bw.write(s+"\r\n");
			bw.flush();
		}
		bw.close();
		br.close();
	}
	
	/**
	 * 读取文件，获取字符串
	 * @param sourceFilePath 源文件地址
	 * @return 返回文件里的字符串
	 * @throws IOException 强制调用函数捕获异常
	 */
	public static String readerString(String sourceFilePath) throws IOException{
		BufferedReader br = new BufferedReader(new FileReader(sourceFilePath));
		String readStr;
		String targetStr = "";//输出字符串
		while((readStr = br.readLine()) != null){
			targetStr += readStr + "\r\n";
		}
		br.close();
		return targetStr;
	}
	
	/**
	 * 将字符串输出到指定目录
	 * @param targetFilePath 输出文件地址
	 * @param context 输出的字符串
	 * @throws IOException 强制调用函数捕获异常
	 */
	public static void writerString(String targetFilePath, String context) throws IOException{
		BufferedWriter bw = new BufferedWriter(new FileWriter(targetFilePath));
		bw.write(context);
		bw.flush();
		bw.close();
	}
	
	public static void main(String[] args) {
		String sourceFilePath = "src\\sources\\再别康桥_徐志摩.txt";
		String targetFilePath = "C:\\Users\\lenovo\\Desktop\\temp.doc";
		try {
			byteReadAndWriter(sourceFilePath, targetFilePath);
//			charReadAndWriter(sourceFilePath, targetFilePath);
//			writerString(targetFilePath, readerString(sourceFilePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
